<?php 
$this->load->view('admin/layout/head', array('title'=>"用户管理",'nav'=>'user'), FALSE);
?>

<div id="container">
	<form class="form-inline" id="search-form" method="GET" action="">
	  <div class="form-group">
	  	<label for=""><?= $this->lang->line('用户名') ?></label>
	  	<input type="text" name="username" class="form-control">
	  </div>

	  <button class="btn btn-info search-btn"><?= $this->lang->line('search') ?></button>
	  <a class="create btn btn-primary"><?= $this->lang->line('create') ?></a>
	</form>
	<div class="table-container mt-10">
		<table class="table table-bordered table-striped table-hover scroll-table">
			<thead >
				<tr>
					<th>id</th>
					<th><?= $this->lang->line('用户名') ?></th>
					<th><?= $this->lang->line('status') ?></th>
					<th><?= $this->lang->line('创建时间') ?></th>	
					<th><?= $this->lang->line('actions'); ?></th>
				</tr>
			</thead>
			<tbody class="text-center">
				<?php foreach ($page as $key => $value): ?>
					<tr id="<?= $value['id'] ?>">
						<td><?= $value['id'] ?></td>
						<td class="username"><?= $value['username'] ?></td>
						<td><?= $value['status'] ? $this->lang->line('禁用') : $this->lang->line('启用') ?></td>
						<td><?= $value['create_time'] ?></td>
						<td>
							<?php if ($value['status'] == 0): ?>
								<a href="#" class="disable" data-status="1"><?= $this->lang->line('禁用') ?></a>
							<?php endif ?>
							<?php if ($value['status'] == 1): ?>
								<a href="#" class="enable" data-status="0"><?= $this->lang->line('启用') ?></a>
							<?php endif ?>
							<a href="#" class="change-password"><?= $this->lang->line('修改密码') ?></a>
							<a href="/admin/cashier/delete/<?= $value['id'] ?>" class="disable"><?= $this->lang->line('delete') ?></a>
						</td>
					</tr>
				<?php endforeach ?>
			</tbody>
			<?php if ($page->last_page>1): ?>
				<tfoot>
					<td colspan="9" style="text-align:right">
						<?php echo $page->render() ?>
					</td>
				</tfoot>
			<?php endif ?>
		</table>
	</div>
</div>

<div class="modal fade create-modal">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<?= $this->lang->line('create') ?>
			</div>
			<div class="modal-body">
				<form action="">
					<div class="form-group">
						<label for=""><?= $this->lang->line('用户名') ?></label>
						<input type="text" name="username" value="" class="form-control">
					</div>
					<div class="form-group">
						<label for=""><?= $this->lang->line('密码') ?></label>
						<input type="password" name="password" value="" class="form-control">
					</div>
					<div class="form-group">
						<button type="button" class="submit btn btn-primary"><?= $this->lang->line('create') ?></button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

<div class="modal fade password-modal">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<?= $this->lang->line('修改密码') ?>
				<span class="username"></span>
			</div>
			<div class="modal-body">
				<form action="">
					<input type="hidden" class="id">
					<div class="form-group">
						<label for=""><?= $this->lang->line('新密码') ?></label>
						<input type="password" name="password" value="" class="form-control">
					</div>
					<div class="form-group">
						<button type="button" class="submit btn btn-primary"><?= $this->lang->line('save') ?></button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

<?php 
$this->load->view('admin/layout/footer');
?>

<script type="text/javascript">
	$(function(){
		$('.disable,.enable').on('click', function () {
			var status = $(this).attr('data-status');
			var id = $(this).parents('tr').attr('id');
			Toast.loading();
			$.ajax({
				url: '/admin/cashier/set_status/'+id,
				type: 'post',
				data: {
					status: status
				},
				success: function (json) {
					Toast.destory();
					Toast.success('<?= $this->lang->line('success') ?>', function () {
						window.location.reload();
					});
				},
				error: function (json) {
					Toast.error(json.error);
				}
			})
		});

		$('.create').on('click', function () {
			$('.create-modal').modal();
		});
		$('.create-modal .submit').on('click', function () {
			$.ajax({
				url: '/admin/cashier/create',
				type: 'post',
				data: $('.create-modal form').serialize(),
				success: function (json) {
					Toast.success('<?= $this->lang->line('success') ?>', function () {
						window.location.reload();
					});
				},
				error : function (error) {
					Toast.error(error.responseJSON.error);
				}
			});
		});

		$('.change-password').on('click', function () {
			$('.password-modal').modal();
			$('.password-modal .username').html($(this).parents('tr').find('.username').html());
			$('.password-modal .id').val($(this).parents('tr').attr('id'));
		});

		$('.password-modal .submit').on('click', function () {

			$.ajax({
				url: '/admin/cashier/reset_password/'+$('.password-modal .id').val(),
				type: 'post',
				data: $('.password-modal form').serialize(),
				success: function () {
					Toast.success('<?= $this->lang->line('success'); ?>', function () {
						window.location.reload();
					});
				}
			})
		});
		var hash = window.location.hash;
		if(hash){
			$(hash).css('background-color','#ccc');
		}
	})
</script>















